package filenavigation

import (
	"fmt"
	"github.com/gogf/gf/errors/gerror"
	"gitlab.local/DO-module/new-filemanage-module/src/controller/errcode"
	"gitlab.local/DO-module/new-filemanage-module/src/service/file_safeBox"
	fileoperate2 "gitlab.local/TerraMaster/tos-modules/fileoperate"
	"gitlab.local/golibrary/user"
	"gitlab.local/golibrary/utils"
	"os"
)

// IsMount 是否挂载
func (n *FileNav) IsMount(safeBoxDir string) bool {
	return n.navigation.safeBox.IsMount(safeBoxDir)
}

// SafeBoxUnLock 保险箱解锁
func (n *FileNav) SafeBoxUnLock(safeBoxDir string) error {
	return n.navigation.safeBox.SafeBoxUnLock(safeBoxDir)
}

// SafeBoxLock 保险箱加锁
func (n *FileNav) SafeBoxLock(safeBoxDir string) error {
	return n.navigation.safeBox.SafeBoxLock(safeBoxDir)
}

// SafeBoxToDir 移出保险箱文件到目录
// path 如果path为空则默认/home/%s/safe_out
// return 1未开启保险箱,2path错误 错误
func (n *FileNav) SafeBoxToDir(uname string, paths []string, outOir string) (uint8, error) {
	lookup, err := user.Lookup(uname)
	if err != nil {
		return 0, err
	}

	mountSafeBoxDir := fmt.Sprintf(file_safeBox.MountSafeBoxDir, lookup.HomeDir)
	if !n.navigation.safeBox.IsMount(mountSafeBoxDir) {
		return 1, nil
	}

	if outOir == "" {
		outOir = fmt.Sprintf(file_safeBox.OutSafeBoxDir, lookup.HomeDir)
		if !utils.Exists(outOir) {
			if err = os.Mkdir(outOir, 0700); err != nil {
				return 0, gerror.Wrap(err, "SafeBoxToDir")
			}
		}
	}

	if err = n.SafeBoxUnLock(mountSafeBoxDir); err != nil {
		return 0, gerror.Wrap(err, "SafeBoxToDir1")
	}

	_, err = n.fOptGetter.MoveFiles(uname, paths, outOir, fileoperate2.Move, true)
	_ = n.SafeBoxLock(mountSafeBoxDir)
	if err != nil {
		return 0, gerror.Wrap(err, "SafeBoxToDir2")
	}

	return 0, nil
}

func (n *FileNav) SetSafeBoxPwd(uname, pwd string) error {
	return n.navigation.safeBox.SetSafeBoxPwd(uname, pwd)
}

func (n *FileNav) GetSafeBoxConf(uname string) (bool, bool, string, error) {
	return n.navigation.safeBox.GetSafeBoxConf(uname)
}

func (n *FileNav) CloseSafeBox(uname string, force bool) (int, error) {
	return n.navigation.safeBox.CloseSafeBox(uname, force)
}

func (n *FileNav) OpenSafeBox(uname string, pwd string, image string, imageId string) (interface{}, error) {
	return n.navigation.safeBox.OpenSafeBox(uname, pwd, image, imageId)
}

func (n *FileNav) GetSafeBoxPath(uname string) string {
	return n.navigation.safeBox.GetSafeBoxPath(uname)
}

// ResetSafe
// @Description: 重置保险箱
// @receiver m
// @param uname
// @param userPassword
// @return error
func (n *FileNav) ResetSafe(uname, userPassword string) (interface{}, error) {
	err := n.navigation.safeBox.MiddleWare.AuthService(uname, userPassword)
	if err != nil {
		return nil, gerror.NewCode(errcode.AuthenticationFailed, "verifyPasswd failed")
	}
	return n.navigation.safeBox.ResetSafeBox(uname), nil
}

// ResetVerificationCode
// @Description: 重置验证码
// @receiver m
// @param uname
// @param userPassword
// @return error
func (n *FileNav) ResetVerificationCode() interface{} {
	return n.navigation.safeBox.ResetVerificationCode()
}
